package ru.maxmatveev.dyndao.demo;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hsqldb.util.DatabaseManager;
import ru.maxmatveev.dyndao.Ordering;
import ru.maxmatveev.dyndao.Pagination;
import ru.maxmatveev.dyndao.Restriction;
import ru.maxmatveev.dyndao.demo.dao.ChildDao;
import ru.maxmatveev.dyndao.demo.dao.ParentDao;
import ru.maxmatveev.dyndao.demo.dao.SomeEntityDao;
import ru.maxmatveev.dyndao.demo.model.Child;
import ru.maxmatveev.dyndao.demo.model.Parent;
import ru.maxmatveev.dyndao.demo.model.SomeEntity;
import ru.maxmatveev.dyndao.impl.hibernate.DaoFactory;

import java.util.Collection;
import java.util.List;

/**
 * @author Max Matveev
 *         Date: Dec 20, 2008
 *         Time: 10:59:50 PM
 */
public class Demo {
    /**
     * Main method.
     *
     * @param args arguments
     */
    @SuppressWarnings({"UnusedAssignment", "UnusedDeclaration"})
    public static void main(String[] args) {
        SessionFactory sessionFactory = new AnnotationConfiguration()
                .addAnnotatedClass(SomeEntity.class)
                .addAnnotatedClass(Parent.class)
                .addAnnotatedClass(Child.class)
                .setProperty("hibernate.connection.driver_class", "org.hsqldb.jdbcDriver")
                .setProperty("hibernate.connection.url", "jdbc:hsqldb:mem:.")
                .setProperty("hibernate.connection.username", "sa")
                .setProperty("hibernate.connection.password", "")
                .setProperty("hibernate.connection.useUnicode", "true")
                .setProperty("hibernate.connection.characterEncoding", "UTF-8")
                .setProperty("hibernate.connection.charSet", "UTF-8")
                .setProperty("hibernate.dialect",
                        "org.hibernate.dialect.HSQLDialect")
                .setProperty("hibernate.show_sql", "true")
                .setProperty("hibernate.hbm2ddl.auto", "create-drop")
                .setProperty("hibernate.current_session_context_class", "thread")
                .buildSessionFactory();

        sessionFactory.getCurrentSession().beginTransaction();
        SomeEntityDao someEntityDao = (SomeEntityDao) DaoFactory.getInstance(SomeEntityDao.class, SomeEntity.class, sessionFactory);

        List<SomeEntity> entities = someEntityDao.getAll();
        if (entities.size() == 0) {
            for (int i = 0; i < 10; i++) {
                SomeEntity entity = new SomeEntity();
                entity.setValue1("value 1: " + i);
                entity.setValue2("value 2: " + (i * 2));
                someEntityDao.save(entity);
            }
        }
        entities = someEntityDao.getByValue1Substring("0");
        SomeEntity entity = entities.get(0);
        entity.setValue1("bugoga");
        entity.setValue2("muhoha");
        entities = someEntityDao.getByIdOrValue(Restriction.or(Restriction.eq("id", 2L), Restriction.and(Restriction.ilike("value1", "4"), Restriction.ilike("value2", "4"))));
        entities = someEntityDao.getAllSortedAndPaginated(new Ordering("value1").Order("value2", Ordering.Direction.DESC), new Pagination(3, 3));
        Integer count = someEntityDao.getCount();
        Parent parent = new Parent();
        ParentDao parentDao = (ParentDao) DaoFactory.getInstance(ParentDao.class, Parent.class, sessionFactory);
        ChildDao childDao = (ChildDao) DaoFactory.getInstance(ChildDao.class, Child.class, sessionFactory);
        parentDao.save(parent);
        parent.setName("Parent #1");
        Parent p2 = new Parent();
        p2.setName("qwer");
        parentDao.save(p2);
        for (int i = 0; i < 20; i++) {
            Child child = new Child();
            child.setName("Child #" + i);
            childDao.save(child);
            parent.getChildren().add(child);
            p2.getChildren().add(child);
        }

        Child child = new Child();
        child.setName("Child ###");
        child.setId(55);
        childDao.save(child);

        count = childDao.getCountByParentId(parent.getId());
        System.out.println(count);
        Collection<Child> children = childDao.getByParentIdPaginated(parent.getId(), new Pagination(5, 5));
        sessionFactory.getCurrentSession().flush();
        children = childDao.getByParentPaginatedAndSortedByName(Restriction.or(Restriction.eq("id", parent.getId()), Restriction.eq("id", p2.getId())), new Pagination(3, 6));
        entities.get(2).setValue2("qwerwqer");
        entity = someEntityDao.getById(5L);
        entity.setValue1("zzzzzzzzzz");
        sessionFactory.getCurrentSession().getTransaction().commit();
        sessionFactory.getCurrentSession().close();

        DatabaseManager.main(new String[]{});
    }

}
